import { ApiRequest } from '@/interface/request';
import { MyRootThunkAction } from '@/interface/store';
import { Token } from '@/interface/token';
import { removeToken, setToken } from '@/utils/token';
import axios from '@/utils/request';
type LoginParams = {
    username?: string;
    password?: string;
    autoLogin?: boolean;
    type?: string;
};
/**
 * 用户登录接口
 * @param dataStr
 * @returns
 */
export const login = (dataStr: LoginParams): MyRootThunkAction => {
    return async (dispatch) => {
        const res = await axios.post<ApiRequest<Token>>('/api/login', dataStr);
        if (res.data.code === '200') {
            setToken(res.data.data);
            dispatch({
                type: 'login/getToken',
                payload: res.data.data,
            });
        }
        return res.data;
    };
};

/**
 * 退出登录
 */
export const logout = (): MyRootThunkAction => {
    return async (dispatch) => {
        // 删除本地token
        removeToken();
        dispatch({
            type: 'logout/removeToken',
            payload: '',
        });
    };
};
